<template>
  <div class="wrapper">
    <div class="registerBack-image" style="text-align: center">
      <el-card style="width: 450px; border: 0px; bottom: 0px" class="register-image">
        <!-- 账号密码注册表单 【Vue3特性，:model和ref不能同名】 -->
        <el-form :model="registerForm" :rules="registerRules" ref="userForm" label-width="80px">
          <!-- 用户名 -->
          <el-form-item prop="username" label="账号" class="el-form-item">
            <el-input placeholder="请输入账号" clearable v-model="registerForm.name">
            </el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password" label="密码" class="el-form-item">
            <el-input placeholder="请输入密码" type="password" show-password v-model="registerForm.password">
            </el-input>
          </el-form-item>
          <!-- 确认密码 -->
          <el-form-item prop="confirmPassword" label="确认密码" class="el-form-item">
            <el-input placeholder="请确认密码" type="password" show-password v-model="registerForm.confirmPassword">
            </el-input>
          </el-form-item>
          <!-- 昵称 -->
          <el-form-item prop="nickname" label="昵称">
            <el-input placeholder="请输入昵称" v-model="registerForm.nickname">
            </el-input>
          </el-form-item>
          <!-- 真实姓名 -->
          <el-form-item prop="realName" label="姓名">
            <el-input placeholder="请输入真实姓名" v-model="registerForm.realName">
            </el-input>
          </el-form-item>
          <!-- 手机号 -->
          <el-form-item prop="phoneNumber" label="手机号">
            <el-input placeholder="请输入手机号" v-model="registerForm.phoneNumber">
            </el-input>
          </el-form-item>
          <!-- 单位 -->
          <el-form-item prop="institutionName" label="单位">
            <el-select clearable v-model="registerForm.institutionName" placeholder="请选择单位" style="width: 100%">
              <!-- <el-option
                v-for="item in institutions"
                :key="item.institutionId"
                :label="item.name"
                :value="item.name"
              ></el-option> -->
              <el-option v-for="item in institutions" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <!-- 邮箱 -->
          <el-form-item prop="email" label="邮箱">
            <el-input placeholder="请输入邮箱" v-model="registerForm.email">
            </el-input>
          </el-form-item>
          <el-form-item class="login_btn">
            <el-button type="primary" autocomplete="off" @click="register">注册</el-button>
            <el-button type="warning" autocomplete="off" @click="$router.push('/worker/login')">返回登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";

const registerForm = ref({
  name: "",
  password: "",
  confirmPassword: "",
  nickname: "",
  realName: "",
  phoneNumber: "",
  institutionName: "",
  email: "",
});

const institutions = ref(["单位1", "单位2", "单位3"]);

// 对确认密码进行验证
const equalToPassword = (rule, value, callback) => {
  if (value !== registerForm.value.password) {
    callback(new Error("两次输入密码不一致!"));
  } else {
    callback();
  }
};

const registerRules = {
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请再次输入密码", trigger: "blur" },
    { required: true, validator: equalToPassword, trigger: "blur" },
  ],
  nickname: [
    { required: true, message: "请输入昵称", trigger: "blur" },
    { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" },
  ],
  realName: [
    { required: true, message: "请输入真实姓名", trigger: "blur" },
    { min: 1, max: 20, message: "长度在1到20个字符", trigger: "blur" },
  ],
  phoneNumber: [
    { required: true, message: "请输入11位的联系方式", trigger: "blur" },
    { min: 11, max: 11, message: "手机号为11位", trigger: "blur" },
  ],
  institutionName: [{ required: true, message: "请选择单位", trigger: "blur" }],
  registrationUserName: [
    { required: true, message: "请输入注册人名", trigger: "blur" },
    { min: 1, max: 20, message: "长度在3到20个字符", trigger: "blur" },
  ],
  email: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    { min: 1, max: 20, message: "请填写邮箱账号", trigger: "blur" },
  ],
};
</script>
<style></style>
